#!/usr/bin/env python
# ########################################
# TESTS
# ########################################

import unittest
from glassvine.properties import props

class test_properties_helper(unittest.TestCase):

    def setUp(self):
        self.myProps = props()

    def test_properties_loading(self):
        fake = []
        fake.append("junk")
        fake.append("port = 8080")
        fake.append("port = 8080 \n")
        fake.append(" database_file =     :memory:")
        fake.append("max_database_size = 150")
        fake.append("# Test comment") # Should not be parsed!
        fake.append("peers = 127.0.0.1")
        fake.append("peers = 127.0.0.2")
        fake.append("peers = 127.0.0.3")

        result = self.myProps._parse_properties(fake)

        self.assert_(result)
        self.assert_(len(result) == 6)

        self.assertEqual(result['port'], "8080")
        self.assertEqual(result['database_file'], ":memory:")
        self.assertEqual(result['max_database_size'], "150")
        self.assert_(result['peers'])
        peers = result['peers']
        self.assert_("127.0.0.1" in peers)
        self.assert_("127.0.0.2" in peers)
        self.assert_("127.0.0.3" in peers)


    def test_rules_loading(self):
        fake = []
        fake.append("cache .*\.png for 1h")
        fake.append("cache .*\.jpg for 2300")
        
        fake.append("don't cache .*\.html")

        cache, dontcache = self.myProps._parse_rules(fake)
        
        self.assert_(cache)
        
        self.assertEqual(cache['.*\.jpg'], 2300)
        self.assertEqual(cache['.*\.png'], 3600)
        
        self.assert_('.*\.html' in dontcache)
        
        
    def test_time_words_loading(self):
        fake = []
        fake.append("time long 1h")
        fake.append("time short 1")
        fake.append("cache .*\.png for long")
        
        cache, _ = self.myProps._parse_rules(fake)
        
        self.assert_(cache)
        self.assertEquals(cache['.*\.png'], 3600)
        self.assertEquals(self.myProps.time_words['short'], 1)
        
    def test_rules_presence_when_loading_props(self):
        fake = []
        fake.append("junk")
        fake.append("port = 8080")
        fake.append("port = 8080 \n")
        fake.append(" database_file =     :memory:")
        fake.append("max_database_size = 150")
        fake.append("# Test comment") # Should not be parsed!
        fake.append("peers = 127.0.0.1")
        fake.append("peers = 127.0.0.2")
        fake.append("peers = 127.0.0.3")
        fake.append("time long 1h")
        fake.append("time short 1")
        
        fake.append("cache .*\.png for long")
        fake.append("cache .*\.png for 1h")
        fake.append("cache .*\.jpg for 2300")
        
        result = self.myProps._parse_properties(fake)
        self.assert_(result)
        
        self.assert_(result['cache_rules'])
        cache_rules = result['cache_rules']
        self.assertEquals(cache_rules['.*\.jpg'], 2300)
        self.assertEquals(cache_rules['.*\.png'], 3600)
        
    def test_properties_default_loading(self):
        properties_obj = props()
        config = properties_obj.load_properties('cache.conf')
        self.assert_(config)
        
        cache_rules = config['cache_rules']
        self.assert_(cache_rules)
        
    def test_parse_time(self):
        times = {}
        times['1d'] = 43200
        times['1h'] = 3600
        times['1m'] = 60
        times['1s'] = 1
        times['1'] = 1
        times['1\n'] = 1
        
        for k, v in times.iteritems():
            self.assert_(self.myProps._parse_time(k), v)
        

if __name__ == "__main__" :
    unittest.main()


